﻿@using ControleRepresentante.Logic.Models;
@using WebSite.ViewModels.EditViewModels;
@using ControleRepresentante.Common;
@using ControleRepresentante.Logic;

@model UsuarioEditViewModel
           
<script language='javascript' type='text/javascript'>
   function SelectUnselectAllRow(name)
   {
       var isCheck = $('#' + name).is(':checked');
       var checkBoxes = $("[id ^= '" + name + "_']");
       checkBoxes.each(function(index)
       {
          checkBoxes[index].checked = isCheck;
       });
   }
   function SelectUnselectAllColumn(name)
   {
       var isCheck = $('#' + name).is(':checked');
       var checkBoxes = $("[id $= '_" + name + "']");
       checkBoxes.each(function (index)
       {
          checkBoxes[index].checked = isCheck;
       });
   }
   function SelectUnselectAll(name)
   {
       var isCheck = $('#' + name).is(':checked');
       var checkBoxes = $(':checkbox');
       checkBoxes.each(function(index)
       {
          checkBoxes[index].checked = isCheck;
       });
   }
</script>

<table style="border-color:Black; border-width:1px; border-style:solid; border-spacing:0px; border-collapse:collapse;" border="1" cellspacing="0" cellpadding="3">
    <tr style="background-color:Gray; color:White;">
        <td></td>
        <td width="60" align="center"></td>
        @{ Dictionary<Operacao, bool> isOperacaoRelevant = new Dictionary<Operacao, bool>(); }
        @foreach (Operacao operacao in Enum.GetValues(typeof(Operacao)))
        {
            isOperacaoRelevant[operacao] = Model.IsOperacaoRelevant(operacao);
            if (operacao != Operacao.None && isOperacaoRelevant[operacao])
            {
                <td width="60" align="center">@operacao.GetDescription()</td>
            }
        }
    </tr>
    <tr>
        <td style="background-color:Gray; color:White;"></td>
        <td align="center" style="background-color:LightGray;">@Html.CheckBox("SelectAll", false, new { onclick = "javascript:SelectUnselectAll(this.id);" })</td>
        @foreach (Operacao operacao in Enum.GetValues(typeof(Operacao)))
        {
            if (operacao != Operacao.None && isOperacaoRelevant[operacao])
            {
                <td align="center" style="background-color:LightGray;">@Html.CheckBox(operacao.ToString(), false, new { onclick = "javascript:SelectUnselectAllColumn(this.id);" })</td>
            }
        }
    </tr>
    @foreach (Area area in AreaOperacaoRegistry.GetAreas())
    {
        if (Model.IsAreaRelevant(area))
        {
            Permissao permissao = Model.GetPermissao(area);
            Operacao operacoes = permissao == null ? Operacao.None : permissao.Operacao;
            <tr>
                <td style="background-color:Gray; color:White;">@area.GetDescription()</td>
                @{ IEnumerable<Operacao> operacoesInArea = Model.GetOperacoes(area); }
                <td align="center" style="background-color:LightGray;">@Html.CheckBox(area.ToString(), false, new { onclick = "javascript:SelectUnselectAllRow(this.id);" })</td>
                @foreach (Operacao operacao in Enum.GetValues(typeof (Operacao)))
                {
                    if (operacao != Operacao.None && isOperacaoRelevant[operacao])
                    {
                        bool value = (operacoes & operacao) == operacao;
                        <td align="center">
                        @if (operacoesInArea.Contains(operacao))
                        {
                            @Html.CheckBox(area + "_" + operacao, value)
                        }
                        </td>
                    }
                }
            </tr>
        }
    }
</table>
